define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {

	//用户类型
	httpService.post(app.urls.dictionary_selectByRule,{fk_rule:"SJAAAAA13"},function(data){
		if("0000" === data.code){
			$scope.userTypeList = data.data;
		}else{
			alert(data.msg);
		}
	});
	//设备类型
	httpService.post(app.urls.dictionary_selectByRule,{fk_rule:"SJAAAAA14"},function(data){
		if("0000" === data.code){
			$scope.appTypeList = data.data;
		}else{
			alert(data.msg);
		}
	});
	$scope.form.accept="gif,jpeg,jpg,png";
	//课程封面上传
	$("#"+$scope.controllerName+" .file_input").change(function() {
		var docObj = $(this)[0];
		var files = docObj.files;

		var fileName = files[0].name;
		var fileType = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length);

		if($scope.form.accept.indexOf(fileType) == -1) {
			alert("请上传"+$scope.form.accept+"类型的文件。");
			return;
		}

		var formData = new FormData(document.getElementById("uploadForm"));
		formData.append("formData", JSON.stringify($scope.form));

		$.ajax({
			type: "POST",
			enctype:'multipart/form-data',
			url: app.urls.app_upload,
			data: formData,
			cache:false,
			processData:false,
			contentType:false,
			success: function(result) {
				$scope.form.icon = result;
				$scope.$apply();
			},
			error:function(result) {
				alert("上传出错。");
			}
		});
	});
	$scope.saveForm = function(){
		var users = [];
        $('#'+$scope.controllerName+' input[name="userPk"]:checked').each(function(){
        	users.push($(this).val());
        });
        if(users.length==0){
        	alert("请选择至少一个使用用户");
        	return;
        }
        $scope.form.userType = JSON.stringify(users);
        
        var types = [];
        $('#'+$scope.controllerName+' input[name="typePk"]:checked').each(function(){
        	types.push($(this).val());
        });
        if(types.length==0){
        	alert("请选择至少一个设备类型");
        	return;
        }
        $scope.form.appType = JSON.stringify(types);
        
		if($scope.formCheck()){
			httpService.post(app.urls.app_insert,$scope.form,function(data){
				if("0000" === data.code){
					eventBusService.publish("content.page.reload",data.data);
					eventBusService.publish("modal.page.close",{});
	    		}else{
	    			alert(data.msg);
	    		}
			});
		}
	}
            
}}});